
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>pwd --- 用户密码数据库 &#8212; Python 3.7.3 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.3 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="spwd --- The shadow password database" href="spwd.html" />
    <link rel="prev" title="posix --- The most common POSIX system calls" href="posix.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/pwd.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="spwd.html" title="spwd --- The shadow password database"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="posix.html" title="posix --- The most common POSIX system calls"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-pwd">
<span id="pwd-the-password-database"></span><h1><a class="reference internal" href="#module-pwd" title="pwd: The password database (getpwnam() and friends). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pwd</span></code></a> --- 用户密码数据库<a class="headerlink" href="#module-pwd" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>此模块可以访问 Unix 用户账户名及密码数据库，在所有 Unix 版本上均可使用。</p>
<p>密码数据库中的条目以元组对象返回，属性对应 <code class="docutils literal notranslate"><span class="pre">passwd</span></code> 中的结构（属性如下所示，可参考 <code class="docutils literal notranslate"><span class="pre">&lt;pwd.h&gt;</span></code>）：</p>
<table border="1" class="docutils">
<colgroup>
<col width="14%" />
<col width="29%" />
<col width="57%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">索引</th>
<th class="head">属性</th>
<th class="head">意义</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_name</span></code></td>
<td>登录名</td>
</tr>
<tr class="row-odd"><td>1</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_passwd</span></code></td>
<td>密码，可能已经加密</td>
</tr>
<tr class="row-even"><td>2</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_uid</span></code></td>
<td>用户 ID 数值</td>
</tr>
<tr class="row-odd"><td>3</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_gid</span></code></td>
<td>组 ID 数值</td>
</tr>
<tr class="row-even"><td>4</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_gecos</span></code></td>
<td>用户名或备注</td>
</tr>
<tr class="row-odd"><td>5</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_dir</span></code></td>
<td>用户主目录</td>
</tr>
<tr class="row-even"><td>6</td>
<td><code class="docutils literal notranslate"><span class="pre">pw_shell</span></code></td>
<td>用户的命令解释器</td>
</tr>
</tbody>
</table>
<p>其中 uid 和 gid 是整数，其他是字符串，如果找不到对应的项目，抛出 <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> 异常。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last" id="index-0">传统的 Unix 系统中，<code class="docutils literal notranslate"><span class="pre">pw_passwd</span></code> 的值通常使用 DES 导出的算法加密（参阅 <a class="reference internal" href="crypt.html#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> 模块）。不过现在的 unix 系统使用 <em>影子密码</em> 系统。在这些 unix 上，<em>pw_passwd</em> 只包含星号（<code class="docutils literal notranslate"><span class="pre">'*'</span></code>）或字母（<code class="docutils literal notranslate"><span class="pre">'x'</span></code>），而加密的密码存储在文件 <code class="file docutils literal notranslate"><span class="pre">/etc/shadow</span></code> 中，此文件不是全局可读的。在 <em>pw_passwd</em> 中是否包含有用信息是系统相关的。如果可以访问到加密的密码，就需要使用 <a class="reference internal" href="spwd.html#module-spwd" title="spwd: The shadow password database (getspnam() and friends). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">spwd</span></code></a> 模块了。</p>
</div>
<p>本模块定义如下内容：</p>
<dl class="function">
<dt id="pwd.getpwuid">
<code class="descclassname">pwd.</code><code class="descname">getpwuid</code><span class="sig-paren">(</span><em>uid</em><span class="sig-paren">)</span><a class="headerlink" href="#pwd.getpwuid" title="永久链接至目标">¶</a></dt>
<dd><p>给定用户的数值 ID，返回密码数据库的对应项目。</p>
</dd></dl>

<dl class="function">
<dt id="pwd.getpwnam">
<code class="descclassname">pwd.</code><code class="descname">getpwnam</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#pwd.getpwnam" title="永久链接至目标">¶</a></dt>
<dd><p>给定用户名，返回密码数据库的对应项目。</p>
</dd></dl>

<dl class="function">
<dt id="pwd.getpwall">
<code class="descclassname">pwd.</code><code class="descname">getpwall</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pwd.getpwall" title="永久链接至目标">¶</a></dt>
<dd><p>返回密码数据库中所有项目的列表，顺序不是固定的。</p>
</dd></dl>

<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="last docutils">
<dt>模块 <a class="reference internal" href="grp.html#module-grp" title="grp: The group database (getgrnam() and friends). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">grp</span></code></a></dt>
<dd>针对用户组数据库的接口，与本模块类似。</dd>
<dt>模块 <a class="reference internal" href="spwd.html#module-spwd" title="spwd: The shadow password database (getspnam() and friends). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">spwd</span></code></a></dt>
<dd>针对影子密码数据库的接口，与本模块类似。</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="posix.html"
                        title="上一章"><code class="docutils literal notranslate"><span class="pre">posix</span></code> --- The most common POSIX system calls</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="spwd.html"
                        title="下一章"><code class="docutils literal notranslate"><span class="pre">spwd</span></code> --- The shadow password database</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/pwd.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="spwd.html" title="spwd --- The shadow password database"
             >下一页</a> |</li>
        <li class="right" >
          <a href="posix.html" title="posix --- The most common POSIX system calls"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" >Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2019, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 4月 09, 2019.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>1.8.4 创建。
    </div>

  </body>
</html>